<template>
  <div class="login">
    <div class="container">
      <!-- 头像 -->
      <img class="avatar" src="@/assets/qq.jpg" alt="" />
      <!-- 输入框 -->
      <el-form
        :model="postLogin"
        :rules="rules"
        ref="postLogin"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入11位手机号码"
            prefix-icon="el-icon-user-solid"
            v-model="postLogin.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入3~16位密码"
            prefix-icon="el-icon-key"
            v-model="postLogin.password"
          ></el-input>
        </el-form-item>
        <el-button @click="getuserList" style="width: 100%" type="primary" plain
          >成功按钮</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from "@/apis/user";
export default {
  data() {
    return {
      // 用户数据
      postLogin: {
        username: "15918983683",
        password: "15918983683",
      },
      //验证规则
      rules: {
        username: [
          { required: true, message: "请输入11位电话号码", trigger: "blur" },
          {
            pattern: /^1[35789]\d{9}$/,
            message: "请输入11位电话号码",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入3~16位密码", trigger: "blur" },
          {
            pattern: /^.{3,16}$/,
            message: "请输入3~16位密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    getuserList() {
      // 如果通过验证规则，valid 返回 true ，否则返回 false
      this.$refs.postLogin.validate(async (valid) => {
        // 如果复合验证规则，发请求
        if (valid) {
          // 发请求取数据
          let res = await userLogin(this.postLogin);
          //   console.log(res);
          if (res.data.message == "登录成功") {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            localStorage.setItem("toutiaoBack", res.data.data.token);
            this.$router.push({ name: "index" });
          } else {
            this.$message.error("用户名或密码错误");
          }
        } else {
          // 不符合验证规则，提示，不发请求
          this.$message.error("用户名或密码输入不合法");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>